/*
 * Copyright (C) 2006-2007 Eskil Bylund
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;

using Gtk;
using Mono.Unix;

using DCSharp.Backend.Objects;

namespace DCSharp.Gui
{
	public class UploadView : TransferView
	{
		public UploadView(TreeModel model) : base(model)
		{
			// Columns
			TreeViewColumn column;
			CellRendererText textRenderer;

			// Started
			textRenderer = new CellRendererText();
			InsertColumn(0, Catalog.GetString("Started"), textRenderer,
				"text", (int)UploadStore.Column.Started);
			column = GetColumn(0);
			column.SortColumnId = (int)UploadStore.Column.Started;
			column.SetCellDataFunc(textRenderer, (Gtk.TreeCellDataFunc)SetTextStyle);

			// Directory
			textRenderer = new CellRendererText();
			textRenderer.Ellipsize = Pango.EllipsizeMode.Middle;
			InsertColumn(2, Catalog.GetString("Directory"), textRenderer,
				"text", (int)UploadStore.Column.Path);
			column = GetColumn(2);
			column.Expand = true;
			column.Resizable = true;
			column.SortColumnId = (int)TransferStoreColumn.Directory;
			column.SetCellDataFunc(textRenderer, (Gtk.TreeCellDataFunc)SetTextStyle);

			// User
			textRenderer = new CellRendererText();
			InsertColumn(3, Catalog.GetString("User"), textRenderer,
				"text", (int)UploadStore.Column.User);
			column = GetColumn(3);
			column.Resizable = true;
			column.SortColumnId = (int)UploadStore.Column.User;
			column.SetCellDataFunc(textRenderer, (Gtk.TreeCellDataFunc)SetTextStyle);
		}

		#region Methods

		public Uploader[] GetSelectedUploads(bool recurse)
		{
			return base.GetSelectedObjects<Uploader>(
				(int)UploadStore.Column.Object, recurse);
		}

		protected override void SetPixbuf(TreeViewColumn column,
			CellRendererPixbuf pixbufRenderer, TreeModel model, TreeIter iter,
			object obj)
		{
			Uploader uploader = (Uploader)obj;

			Gdk.Pixbuf pixbuf = null;
			if (uploader != null)
			{
				pixbuf = uploader.Request.Type == RequestType.File ?
					Util.GetIconFromPath(uploader.LocalFile.FullName) :
					IconManager.GetIcon("package-x-generic", 16);
			}
			pixbufRenderer.Pixbuf = pixbuf;
		}

		protected override void SetStatusText(TreeViewColumn column,
			CellRendererText textRenderer, TreeModel model, TreeIter iter,
			object obj)
		{
			Uploader uploader = (Uploader)obj;

			textRenderer.Text = uploader != null ? Util.GetStatus(uploader) : null;

			SetTextStyle(column, textRenderer, model, iter, obj);
		}

		protected override void SetTextStyle(TreeViewColumn column,
			CellRendererText textRenderer, TreeModel model, TreeIter iter,
			object obj)
		{
			Uploader uploader = (Uploader)obj;
			if (uploader != null && uploader.Active)
			{
				textRenderer.Foreground = "black";
			}
			else if (uploader != null && uploader.Progress == 1)
			{
				textRenderer.Foreground = "dark green";
			}
			else
			{
				textRenderer.Foreground = "dim grey";
			}
		}

		private void SetTextStyle(TreeViewColumn column, CellRenderer renderer,
			TreeModel model, TreeIter iter)
		{
			object obj = model.GetValue(iter, (int)UploadStore.Column.Object);

			SetTextStyle(column, (CellRendererText)renderer, model, iter, obj);
		}

		#endregion
	}
}
